package com.ruoyi.system.service.impl.fxImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.fx.FxServeCategory;
import com.ruoyi.system.domain.vo.FxServeCategoryVo;
import com.ruoyi.system.mapper.fx.FxServeCategoryMapper;
import com.ruoyi.system.service.fx.IFxServeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 商品分类Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-27
 */
@Service
public class FxServeCategoryServiceImpl extends ServiceImpl<FxServeCategoryMapper,FxServeCategory> implements IFxServeCategoryService
{
    @Autowired
    private FxServeCategoryMapper fxServeCategoryMapper;

    /**
     * 查询商品分类
     *
     * @param id 商品分类主键
     * @return 商品分类
     */
    @Override
    public FxServeCategoryVo selectFxServeCategoryById(Long id)
    {
        FxServeCategory fxServeCategory = fxServeCategoryMapper.selectFxServeCategoryById(id);
        if (ObjectUtils.isEmpty(fxServeCategory)){
            return null;
        }
        FxServeCategoryVo fxServeCategoryVo = BeanUtil.copyProperties(fxServeCategory, FxServeCategoryVo.class);
        ArrayList<FxServeCategoryVo> fxServeCategoryVos = new ArrayList<>();
        fxServeCategoryVos.add(fxServeCategoryVo);
        getSon(fxServeCategoryVos);
        return fxServeCategoryVos.get(0);
    }

    /**
     * 查询商品分类列表
     *
     * @param fxServeCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<FxServeCategoryVo> selectFxServeCategoryList(FxServeCategory fxServeCategory)
    {
        List<FxServeCategoryVo> fxServeCategoryVos;
        //搜索子集
        if (StringUtils.isEmpty(fxServeCategory.getCategoryName())){
            fxServeCategoryVos = BeanUtil.copyToList(fxServeCategoryMapper.selectFxServeCategoryList(fxServeCategory),FxServeCategoryVo.class) ;
            this.getSon(fxServeCategoryVos);
        }else {
            //搜索父子集
            fxServeCategoryVos = BeanUtil.copyToList(fxServeCategoryMapper.selectFxServeCategoryList(fxServeCategory),FxServeCategoryVo.class) ;
            Set<FxServeCategoryVo> finalList=new HashSet<>();
            this.getUp(finalList,fxServeCategoryVos);
            List<FxServeCategoryVo> leaseGoodCategoryVos = new ArrayList<>(finalList);
            this.getSon(leaseGoodCategoryVos);
            fxServeCategoryVos=leaseGoodCategoryVos;
        }
        return fxServeCategoryVos;
    }

    //搜索子集
    public void getSon(List<FxServeCategoryVo> fxServeCategoryVos){
        for (FxServeCategoryVo fxServeCategoryVo : fxServeCategoryVos) {
            if (ObjectUtils.isEmpty(fxServeCategoryVo.getParentId())){
                fxServeCategoryVo.setTitle("首页logo");
            }
            if (!ObjectUtils.isEmpty(fxServeCategoryVo.getParentId())){
                fxServeCategoryVo.setTitle("上方轮播图");
            }
            List<FxServeCategory> list = this.lambdaQuery().eq(FxServeCategory::getParentId, fxServeCategoryVo.getId()).list();
            if (!CollectionUtils.isEmpty(list)){
                List<FxServeCategoryVo> item = BeanUtil.copyToList(list, FxServeCategoryVo.class);
                fxServeCategoryVo.setFxServeCategoryVos(item);
                getSon(item);
            }else if (CollectionUtils.isEmpty(list)&&!ObjectUtils.isEmpty(fxServeCategoryVo.getParentId())){
                fxServeCategoryVo.setTitle("服务内容");
            }
        }
    }

    //找最上级
    public void getUp(Set<FxServeCategoryVo> finalList,List<FxServeCategoryVo> fxServeCategoryVos){
        boolean flag = false;
        List<FxServeCategoryVo> temp = new ArrayList<>();
        // 使用迭代器遍历集合
        Iterator<FxServeCategoryVo> iterator = fxServeCategoryVos.iterator();
        while (iterator.hasNext()) {
            FxServeCategoryVo fxServeCategoryVo = iterator.next();

            if (ObjectUtils.isEmpty(fxServeCategoryVo.getParentId())) {
                // 如果没有父级，则添加到最终列表中
                finalList.add(fxServeCategoryVo);
                iterator.remove(); // 从当前列表中移除
            } else {
                // 查询父级分类
                FxServeCategoryVo one = BeanUtil.copyProperties(
                        this.lambdaQuery().eq(FxServeCategory::getId, fxServeCategoryVo.getParentId()).one(),
                        FxServeCategoryVo.class
                );

                if (ObjectUtils.isEmpty(one.getParentId())) {
                    // 如果父级没有更上一级，则添加到最终列表中
                    finalList.add(one);
                } else {
                    flag = true; // 标记需要继续递归
                    // 替换当前元素为父级分类
                    iterator.remove(); // 移除当前元素
                    temp.add(one); // 添加父级分类
                }
            }
        }
        // 如果还有需要处理的元素，则递归调用
        if (flag) {
            fxServeCategoryVos.addAll(temp);
            getUp(finalList, fxServeCategoryVos);
        }
    }



    /**
     * 新增商品分类
     *
     * @param fxServeCategory 商品分类
     * @return 结果
     */
    @Override
    public int insertFxServeCategory(FxServeCategory fxServeCategory)
    {
        fxServeCategory.setCreateTime(DateUtils.getNowDate());
        fxServeCategory.setCreateBy(SecurityUtils.getUserId().toString());
        fxServeCategory.setMoney(new BigDecimal("0"));
        return fxServeCategoryMapper.insertFxServeCategory(fxServeCategory);
    }

    /**
     * 修改商品分类
     *
     * @param fxServeCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateFxServeCategory(FxServeCategory fxServeCategory)
    {
        fxServeCategory.setUpdateTime(DateUtils.getNowDate());
        return fxServeCategoryMapper.updateFxServeCategory(fxServeCategory);
    }

    /**
     * 批量删除商品分类
     *
     * @param ids 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteFxServeCategoryByIds(Long[] ids)
    {
        return fxServeCategoryMapper.deleteFxServeCategoryByIds(ids);
    }

    /**
     * 删除商品分类信息
     *
     * @param id 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteFxServeCategoryById(Long id)
    {
        return fxServeCategoryMapper.deleteFxServeCategoryById(id);
    }

    /**
     * 根据分类id查询所有子分类
     */
    @Override
    public Set<Long> getAllSonId(Long id) {
        HashSet<Long> ids = new HashSet<>();
        ids.add(id);
        selectAllSonId(ids,id);
        return ids;
    }

    //填充所有子分类id
    public void selectAllSonId(Set<Long> ids,Long id){
        List<FxServeCategory> list = this.lambdaQuery().eq(FxServeCategory::getParentId, id).list();
        if (CollectionUtils.isEmpty(list)){
            return;
        }
        Set<Long> tempIds = list.stream().map(FxServeCategory::getId).collect(Collectors.toSet());
        ids.addAll(tempIds);
        for (Long tempId : tempIds) {
            selectAllSonId(ids,tempId);
        }
    }

}
